<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>商品管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/dtree.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/font/dtreefont.css" media="all" />

</head>
<body class="childrenBody">

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 5px;">
    <legend>查询条件</legend>
</fieldset>
<blockquote class="layui-elem-quote">
    <form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form layui-form-pane">
        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="goodsName"    autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">所属供货商</label>
                <div class="layui-input-block">
                    <select name="salerList" id="salerList" lay-verify="" lay-search="">
                        <option value="-1">无</option>
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">秒杀</label>
                <div class="layui-input-block">
                    <input type="radio" name="limitDiscount" value="-1" title="全部">
                    <input type="radio" name="limitDiscount" value="0" title="非秒杀">
                    <input type="radio" name="limitDiscount" value="1" title="秒杀">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">下架商品</label>
                <div class="layui-input-block">
                    <input type="radio" name="sellOff" value="-1" title="全部">
                    <input type="radio" name="sellOff" value="0" title="售卖">
                    <input type="radio" name="sellOff" value="1" title="已下架">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSearch"><span class="layui-icon layui-icon-search"></span>查询</button>
                    <button type="reset" onclick="javascript:document.getElementById('search_deptid').value=''" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
        </div>

    </form>
</blockquote>
<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>
    <div id="roleToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>添加商品</button>
    </div>

    <div id="roleRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-edit"></span>更新</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
    </div>
</div>


<!-- 数据表格结束 -->


<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <input type="text" name="goodsName" lay-verify="required" maxlength="50" autocomplete="off" placeholder="请输入名称" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">品类</label>
            <div class="layui-input-block">
                <select name="goodsType" lay-verify="required" lay-search="">
                    <option value="0">蔬菜</option>
                    <option value="1">肉类</option>
                    <option value="2">水果</option>
                    <option value="3">水产</option>
                    <option value="4">家禽</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属供货商</label>
            <div class="layui-input-block">
                <select name="belongSaler" id="belongSaler" lay-verify="required" lay-search="">

                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">缩略图</label>
                <div class="layui-input-inline">
                    <input type="text" name="goodsImg" id="goodsImg" lay-verify="required" autocomplete="off" placeholder="请输入商品缩略图路径" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">详情图</label>
                <div class="layui-input-inline">
                    <input type="text" name="goodsDetailImg" id="goodsDetailImg" lay-verify="required"  autocomplete="off" placeholder="请输入商品详情图列表" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">原价</label>
                <div class="layui-input-inline">
                    <input type="text" name="rawPrice" id="rawPrice" lay-verify="required/number" autocomplete="off" placeholder="请输入商品原价" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">售价</label>
                <div class="layui-input-inline">
                    <input type="text" name="sellPrice" id="sellPrice" lay-verify="required/number"   autocomplete="off" placeholder="请输入商品售价" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">供货商价格</label>
                <div class="layui-input-inline">
                    <input type="text" name="salerPrice" id="salerPrice" lay-verify="required/number"   autocomplete="off" placeholder="请输入供货商售价" class="layui-input">
                </div>
            </div>


        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">单位</label>
            <div class="layui-input-block">
                <select name="unit" lay-verify="required" lay-search="">
                    <option value="0">克</option>
                    <option value="1">千克</option>
                    <option value="2">个</option>
                    <option value="3">条</option>
                    <option value="4">只</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">起售数量</label>
                <div class="layui-input-inline">
                    <input type="text" name="unitAmount" id="unitAmount" lay-verify="required/number" autocomplete="off" placeholder="请输入商品起售数量" class="layui-input">
                </div>
            </div>

        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">最大购买数量</label>
                <div class="layui-input-inline">
                    <input type="text" name="maxCount" id="maxCount" lay-verify="required/number" autocomplete="off" placeholder="请输入商品最大购买数量" class="layui-input">
                </div>
            </div>

        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">偏差</label>
                <div class="layui-input-inline">
                    <input type="text" name="offWeight" id="offWeight" lay-verify="number" autocomplete="off" placeholder="请输入重量偏差" class="layui-input">
                </div>
            </div>

        </div>

        <div class="layui-form-item">
                <label class="layui-form-label">额外说明</label>
                <div class="layui-input-block">
                    <input type="text" name="comment" id="comment" lay-verify="required" maxlength="100" autocomplete="off" placeholder="请输入额外说明" class="layui-input">
                </div>

        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">是否秒杀</label>
            <div class="layui-input-block">
                <input type="checkbox" name="limitDiscount" id="limitDiscount">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否推荐</label>
            <div class="layui-input-block">
                <input type="checkbox" name="hot" id="valid">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否下架</label>
            <div class="layui-input-block">
                <input type="checkbox" name="sellOff" id="available">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-filter="doSubmit" lay-submit="" id="doSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="button" class="layui-btn layui-btn-warm" id="doReset"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->
<!-- 分配权限的弹出层开始 -->
<div style="display: none;" id="selectRolePermissionDiv">
    <ul id="permissionTree" class="dtree" data-id="0" ></ul>
</div>
<!-- 分配权限的弹出层结束 -->



<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.extend({
        dtree: '/resources/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['jquery','form','table','layer','dtree'],function(){
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var dtree = layui.dtree;
        //初始化富文本
        var editIndex ;//= layedit.build('content');
        //加载 数据
        var tableIns=table.render({
            elem: '#roleTable'
            ,url:'/goods/loadAllGoods'
            ,toolbar: '#roleToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '角色数据表'
            ,height:'full-220'
            ,page: true
            ,cols: [ [
                {field:'goodsName', title:'商品名称',align:'center'}
                ,{field:'belongSalerName', title:'供货商',align:'center'}
                ,{field:'goodsType', title:'品种类型',align:'center',templet:function(d){
                        const goodstype = ["蔬菜","猪肉","牛肉","水产","家禽","水果"];
                        let typeName = goodstype[d.goodsType];
                        return '<font color=green>' + typeName + '</font>';

                    }}

               // ,{field:'goodsImg', title:'商品缩略图',align:'center'}
               // ,{field:'goodsDetailImg', title:'商品详情图',align:'center'}

                ,{field:'rawPrice', title:'商品原价',align:'center'}
                ,{field:'sellPrice', title:'商品售价',align:'center'}
                ,{field:'salerPrice', title:'供货商售价',align:'center'}
                ,{field:'sellCount', title:'已售总数',align:'center'}
                ,{field:'unit', title:'计量单位',align:'center',templet:function(d){
                        const unitType = ["克","千克","个","条","只"];
                        let typeName = unitType[d.unit];
                        return '<font color=black>' + typeName + '</font>';

                    }}
                ,{field:'unitAmount', title:'起售数量',align:'center'}
                ,{field:'offWeight', title:'重量误差',align:'center'}
                ,{field:'comment', title:'说明',align:'center'}
                ,{field:'limitDiscount', title:'是否秒杀',align:'center',templet:function(d){
                        if(d.limitDiscount==1)
                            return '<font color=red>秒杀</font>';
                        else if(d.limitDiscount==0)
                            return '<font color=green>非秒杀</font>';

                    }}

                ,{field:'hot', title:'是否推荐',align:'center',templet:function(d){
                        if(d.hot==1)
                            return '<font color=green>推荐</font>';
                        else if(d.hot==0)
                            return '<font color=red>未推荐</font>';

                    }}

                ,{field:'sellOff', title:'是否下架',align:'center',templet:function(d){
                        if(d.sellOff==1)
                            return '<font color=red>下架</font>';
                        else if(d.sellOff==0)
                            return '<font color=green>在售</font>';

                    }}

                ,{fixed: 'right', title:'操作', toolbar: '#roleRowBar',width:"200",align:'center'}
            ] ]
        });

        //模糊查询
        form.on("submit(doSearch)",function(data){
            tableIns.reload({
                where:data.field,
                page:{
                    curr:1
                }
            });
            return false;
        });

        $("#doReset").click(function () {

            $("#dataFrm")[0].reset();

        })
        //监听工具条的事件
        table.on("toolbar(roleTable)",function(obj){
            switch(obj.event){
                case 'add':
                    openAddLayer();
                    break;
            };
        });


        function getSalerList(data) {

            $.post("/saler/loadAllValidSaler",null,function(res){

                $('#belongSaler').empty();
                $.each(res, function (index, item) {
                    $('#belongSaler').append(new Option(item.salerName, item.id));// 下拉菜单里添加元素
                });

               // $('#salerList').empty();
                $.each(res, function (index, item) {
                    $('#salerList').append(new Option(item.salerName, item.id));// 下拉菜单里添加元素
                });

                layui.form.render("select");//重新渲染 固定写法
            })
        };

        getSalerList();
        //监听行工具条的事件
        table.on("tool(roleTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'update':
                    openUpdateRoleLayer(data);
                    break;
                case 'delete':
                    deleteRole(data);
                    break;
            };
        });

        var mainIndex;
        var url;
        //打开添加的弹出层
        function openAddLayer(){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','600px'],
                title:'添加商品',
                success:function(){

                    $("#dataFrm")[0].reset();
                    url="/goods/addGoods";
                }
            });
        }

        //打开修改的弹出层
        function openUpdateRoleLayer(data){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','600px'],
                title:'修改商品',
                success:function(){

                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    url="/goods/updateGoods";
                }
            });
        }

        form.on("submit(doSubmit)",function(data) {
            data.field.hot =  data.field.hot == "on"?1:0;
            data.field.sellOff =  data.field.sellOff == "on"?1:0;
            data.field.limitDiscount = data.field.limitDiscount == "on"?1:0;

            if(!(data.field.rawPrice >= data.field.sellPrice && data.field.sellPrice >= data.field.salerPrice))
            {
                alert("请检查价格");
                return;
            }
            $.post(url, data.field, function (res) {
                if (res.code == 200) {
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            })
            return false;
        });




        //删除
        function deleteRole(data){
            layer.confirm('你确定要删除【'+data.goodsName+'】吗?', {icon: 3, title:'提示'}, function(index){
                $.post("/goods/deletetGoods",{id:data.id},function(res){
                    if(res.code==200){
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }

    });
</script>

</body>
</html>